  // pages/shop/index.js
  import {
    getSetting,
    chooseAddress,
    openSetting,
    showModal

  } from '../../utils/shopWx'

  Page({

    /**
     * 页面的初始数据
     */
    data: {
      addres: [],
      addrew: [],
      goods: [],
      allcheck: true,
      totelprick: 0,
      totelnum: 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },
    async gouwucheappadd() {
      try {
        const res1 = await getSetting()
        const scopeAddress = res1.authSetting['scope.address']
        if (scopeAddress === false) {
          await openSetting()
        }
        const res2 = await chooseAddress()
        wx.setStorageSync('res2', res2)

      } catch (err) {
        console.log(err);
      }


    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
      const addres = wx.getStorageSync('res2')
      this.setData({
        addres: addres,
        addrew: addres.provinceName + addres.cityName + addres.countyName + addres.detailInfo
      })
      const cart = wx.getStorageSync('cart') || []
      this.setdatacart(cart)
    },
    checkadd() {
      console.log(123123213);
    },
    heardenter(e) {
      const goods_id = e.currentTarget.dataset.id
      console.log(goods_id);
      let cart = this.data.goods
      let index = cart.findIndex(v => v.goods_id === goods_id)
      console.log(index);
      // 选中状态取反
      cart[index].checked = !cart[index].checked
      // 总价格
      this.setdatacart(cart)
    },

    setdatacart(cart) {
      // 总价格
      let totelprick = 0
      let totelnum = 0
      let allcheck = true
      cart.forEach(v => {
        if (v.checked) {
          totelprick += v.num * v.goods_price
          totelnum += v.num
        } else {
          allcheck = false
        }
        allcheck = cart.length != 0 ? allcheck : false

      })
      this.setData({
        goods: cart,
        totelprick,
        totelnum,
        allcheck: allcheck
      })
      wx.setStorageSync('cart', cart)
    },
    qdasdwasd() {
      let cart = this.data.goods
      let allcheck = this.data.allcheck
      allcheck = !allcheck
      cart.forEach(v => v.checked = allcheck)
      this.setData({
        goods: cart,
        allcheck
      })
    },
    // 商品数量的编辑
    async heartegoods_id(e) {
      const {
        id,
        operation
      } = e.currentTarget.dataset
      console.log(id, operation);
      let cart = this.data.goods
      const index = cart.findIndex(v => v.goods_id === id)
      if (cart[index].num === 1 && operation === -1) {
        const res = await showModal({
          content: '您是否要删除?'
        })
        if (res.confirm) {
          cart.splice(index, 1)
          this.setdatacart(cart)
        }

      } else {
        cart[index].num += operation
        this.setdatacart(cart)
      }

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
  })